package com.epalmpay.service.mall.impl;

import com.epalmpay.status.Rescode;
import com.epalmpay.dto.mall.CityOutDTO;
import com.epalmpay.dto.mall.FreightDetailDTO;
import com.epalmpay.dto.mall.FreightTemplateInDTO;
import com.epalmpay.dto.mall.FreightTemplateOutDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.entity.MallFreight;
import com.epalmpay.entity.MallFreightDetail;
import com.epalmpay.mapper.CitiesMapper;
import com.epalmpay.mapper.MallFreightDetailMapper;
import com.epalmpay.mapper.MallFreightMapper;
import com.epalmpay.service.mall.IFreightTemplateService;
import com.epalmpay.util.PropertyCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by lifayi on 2017/8/3.
 */
@Service
public class FreightTemplateServiceImpl implements IFreightTemplateService {

    @Autowired
    private MallFreightMapper mallFreightMapper;
    @Autowired
    private MallFreightDetailMapper mallFreightDetailMapper;
    @Autowired
    private CitiesMapper citiesMapper;

    @Override
    public List<FreightTemplateOutDTO> listFreightTemplate(Long groupId) throws Exception {
        List<FreightTemplateOutDTO> freightTemplateOutDTOList = new ArrayList<>();
        List<MallFreight> mallFreightList = mallFreightMapper.listByGroupId(groupId);
        for(int i = 0; i < mallFreightList.size(); i++) {
            MallFreight mallFreight = mallFreightList.get(i);
            FreightTemplateOutDTO freightTemplateOutDTO = new FreightTemplateOutDTO();
            PropertyCopyUtil.copyProperties(mallFreight, freightTemplateOutDTO);
            freightTemplateOutDTO.setWarehouseAddrQualify(mallFreight.getProvName() + " " + mallFreight.getCityName() + " " + mallFreight.getCounName() + " " + mallFreight.getWarehouseAddr());
            freightTemplateOutDTOList.add(freightTemplateOutDTO);
        }
        return freightTemplateOutDTOList;
    }

    @Override
    public List<FreightDetailDTO> freightDetail(Long freightId, Long groupId) throws Exception {
        List<FreightDetailDTO> freightDetailDTOList = new ArrayList<>();

        List<MallFreightDetail> mallFreightDetailList = mallFreightDetailMapper.getByIdAndGroupId(freightId, groupId);
        for(int i = 0; i < mallFreightDetailList.size(); i++) {
            MallFreightDetail mallFreightDetail = mallFreightDetailList.get(i);
            FreightDetailDTO freightDetailDTO = new FreightDetailDTO();
            PropertyCopyUtil.copyProperties(mallFreightDetail, freightDetailDTO);
            String provincies = mallFreightDetail.getProvinceIds();
            List<Integer> ids = new ArrayList<>();
            for(String idStr : provincies.split(",")) {
                ids.add(Integer.parseInt(idStr));
            }
            List<CityOutDTO> citiesList = citiesMapper.listByIds(ids);
            freightDetailDTO.setCitiesList(citiesList);
            freightDetailDTOList.add(freightDetailDTO);
        }
        return freightDetailDTOList;
    }

    @Override
    @Transactional
    public BaseOutDTO freightDel(Long freightId, Long groupId) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();

        mallFreightMapper.deleteByPrimaryKeyAndGroupId(freightId, groupId);
        mallFreightDetailMapper.deleteByFreightId(freightId);

        baseOutDTO.setErrorCode(Rescode.SUCCESS);
        return baseOutDTO;
    }

    @Override
    public FreightTemplateOutDTO freightTemplateDetail(Long freightId, Long groupId) throws Exception {
        FreightTemplateOutDTO freightTemplateOutDTO = new FreightTemplateOutDTO();

        MallFreight mallFreight = mallFreightMapper.selectWidthAddrDetail(freightId, groupId);
        if(mallFreight != null) {
            PropertyCopyUtil.copyProperties(mallFreight, freightTemplateOutDTO);
            List<FreightDetailDTO> freightDetailDTOList = mallFreightDetailMapper.getByFreightId(freightId);
            for(int i = 0; i < freightDetailDTOList.size(); i++) {
                FreightDetailDTO freightDetailDTO = freightDetailDTOList.get(i);
                List<Integer> citiesIds = new ArrayList<>();
                String[] citiesIdsStr = freightDetailDTO.getProvinceIds().split(",");
                for(String str : citiesIdsStr) {
                    citiesIds.add(Integer.parseInt(str));
                }
                List<CityOutDTO> cityOutDTOList = citiesMapper.listByIds(citiesIds);
                freightDetailDTO.setCitiesList(cityOutDTOList);
            }
            freightTemplateOutDTO.setFreightDetailDTOList(freightDetailDTOList);
        }
        return freightTemplateOutDTO;
    }

    @Override
    @Transactional
    public BaseOutDTO freightAdd(FreightTemplateInDTO freightTemplateInDTO) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        MallFreight mallFreight = new MallFreight();

        Date date = new Date();
        PropertyCopyUtil.copyProperties(freightTemplateInDTO, mallFreight);
        mallFreight.setGmtModified(date);
        int i = 0;
        Long id = mallFreight.getId();
        if(id != null) {
            i = mallFreightMapper.updateByPrimaryKeySelective(mallFreight);
        } else {
            i = mallFreightMapper.insertSelective(mallFreight);
        }

        if(i == 1) {
            if(id != null) {
                mallFreightDetailMapper.deleteByFreightId(id);
            }
            mallFreightDetailMapper.insertBatch(freightTemplateInDTO.getFreightDetailDTOList(), mallFreight.getId());
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    @Override
    public List<Map<String, Object>> listAllFreightNameAndId(Long groupId) {
        return mallFreightMapper.listAllFreightNameAndId(groupId);
    }
}
